<?php

// $Id$


require_once(TANGRA_MAIN_DIR.'interfaces/i_db_storable.class.php');


require_once('tcms_menu.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/vpage/tcms_vpage_dbc.class.php');


class TCMS_Menu_DBC extends TCMS_menu implements I_DB_Storable {
	public function save(DB_Connection $dbc) {
		$ret = false;

		if ($this->get_id()) {
			$ret = $this->update($dbc);
		} else {
			$ret = $this->insert($dbc);
			$this->set_id($ret);

		}

		return $ret;
	}


	public function load_by_id(DB_Connection $dbc, $id) {
		tangra_if_not_int_throw_e($id);

		$ret = false;

		$sql = "select hid, ".
						"name, ".
						"type, ".
						"params ".
					"from tcms_menus where id = $id";
		$rez = $dbc->execute($sql);

		if (!$rez->is_eof()) {
			$rez_obj = $rez->fetch_object();

			$this->set_id($id);
			$this->set_hid(stripslashes($rez_obj->HID));
			$this->set_name(stripslashes($rez_obj->NAME));
			$this->set_type($rez_obj->TYPE);
			$this->set_params(stripslashes($rez_obj->PARAMS));

			$ret = $id;
		}

		return $ret;
	}


	protected function insert(DB_Connection $dbc) {
		$ret = false;

		$id = $dbc->generate_id('tcms_menus_seq');
		$hid = addslashes($this->get_hid());
		$name = addslashes($this->get_name());
		$type = $this->get_type();
		$params = addslashes($this->get_params());

		if ($id) {
			$sql = "insert into tcms_menus (id, ".
									      "hid, ".
									      "name, ".
									      "type, ".
									      "params ".
									      ") ".
								"values ".
									      "($id,".
									      "'$hid', ".
									      "'$name', ".
									      "$type, ".
									      "'$params' ".
									      ")";
			$dbc->execute($sql);
			$ret = $id;
		} else {
			throw new TE_Exception('tcms_menus_seq');
		}

		return $ret;
	}


	protected function update(DB_Connection $dbc) {
		$id = $this->get_id();

		$hid = addslashes($this->get_hid());
		$name = addslashes($this->get_name());
		$type = $this->get_type();
		$params = addslashes($this->get_params());


		$sql = "update tcms_menus set ".
						"hid = '$hid', ".
						"name = '$name', ".
						"type = $type, ".
						"params = '$params' ".
					"where id = ".$id;

		$dbc->execute($sql);
		$ret = $id;

		return $ret;
	}


	public static function delete(DB_Connection $dbc, $id) {
		tangra_if_not_int_throw_e($id);

		$sql = "delete from tcms_menus where id = $id";
		$dbc->execute($sql);
	}


	public static function get_sql_for_grid() {
		$sql = "select m.id, ".
						"m.hid, ".
						"m.name, ".
						"mt.name, ".
						"m.params, ".
						"mt.page_manage_path ".
					"from tcms_menus m, tcms_menu_types mt where m.type = mt.id order by hid asc";

		return $sql;
	}


	public static function	get_sql_count_for_grid() {
		$sql = "select count(id) as total_rows from tcms_menus";

		return $sql;
	}


	public static function load_types_ol_map(DB_Connection $dbc) {
		$sql = "select id, name from tcms_menus order by name asc";
		$rez = $dbc->execute($sql);

		$ret = array();
		$ids = array();

		while($rez_obj = $rez->fetch_object()) {
			array_push($ids, $rez_obj->ID);
			$ret['ol_map'][$rez_obj->ID] = $rez_obj->NAME;
		}

		if ($ids) {
			$ret['ids'] = $ids;
		}

		return $ret;
	}


	public static function is_unique_hid(DB_Connection $dbc, $id, $hid) {
		tangra_if_not_int_throw_e($id);
		$hid = addslashes($hid);

		$sql = "select id from tcms_menus where id <> $id and hid = '$hid'";
		$rez = $dbc->execute($sql);

		return $rez->is_eod();
	}


	public function load_by_hid(DB_Connection $dbc, $hid) {
		$hid = addslashes($hid);
		$ret = false;

		$sql = "select id from tcms_menus where hid = '$hid'";
		$rez = $dbc->execute($sql);
		if (!$rez->is_eod()) {
			$rez_obj = $rez->fetch_object();
			$ret = $this->load_by_id($dbc, $rez_obj->ID);
		}

		return $ret;
	}


	public static function select_menus_for_select(DB_Connection $dbc, $template_id) {
		tangra_if_not_int_throw_e($template_id);
		$sql = "select m.id as id, ".
						"m.name as name ".
					"from tcms_menus m left outer join tcms_vpt_menus vptm ".
							"on m.id = vptm.menu and vptm.template = $template_id ".
					"where vptm.id is NULL ".
					"order by m.name asc";
		$rez = $dbc->execute($sql);
		$ret = array();
		while ($rez_obj = $rez->fetch_object()) {
			$ret[] = array('id' => $rez_obj->ID, 'name' => $rez_obj->NAME);
		}

		return $ret;
	}


	public static function menu_changed(DB_Connection $dbc, $menu_id, $user_id) {
		tangra_if_not_int_throw_e($menu_id);

		$sql = "select vpages.id ".
					"from tcms_vpt_menus vptm, ".
							"tcms_vpages vpages ".
						"where vptm.menu = $menu_id and ".
							"vpages.template = vptm.template";

		$rez = $dbc->execute($sql);
		while ($rez_obj = $rez->fetch_object()) {
			TCMS_VPage_DBC::update_last_modified($dbc, $rez_obj->ID, $user_id);
		}
	}
}
